package com.itheima.controller.checkitem;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.checkitem.CheckItemService;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

/**
 * 检查项的增删改查，分页查询，按id查询
 * @author 陈功炳
 * @since 2021/11/4 11:32
 */

@RestController
@RequestMapping("checkitem")
public class CheckItemController {
    @Reference
    private CheckItemService checkItemService;

    /**
     * 添加检查项
     * @param checkItem 需要添加的检查项数据
     * @return flag和message
     */
    @PostMapping("add.do")
    public Result add(@RequestBody CheckItem checkItem) {
        Boolean flag = checkItemService.add(checkItem);
        return new Result(flag, flag ? MessageConstant.ADD_CHECKITEM_SUCCESS : MessageConstant.ADD_CHECKITEM_FAIL);
    }

    /**
     * 删除检查项
     * @param id 需要删除的检查项id
     * @return flag,message
     */
    @GetMapping("delete.do")
    public Result delete(Integer id) {
        Boolean flag = checkItemService.delete(id);
        return new Result(flag,flag?MessageConstant.DELETE_CHECKITEM_SUCCESS:MessageConstant.DELETE_CHECKITEM_FAIL);
    }

    /**
     * 修改检查项
     * @param checkItem 修改后的的检查项数据
     * @return flag，messa
     */
    @PostMapping("edit.do")
    public Result edit(@RequestBody CheckItem checkItem) {
        Boolean flag = checkItemService.edit(checkItem);
        return new Result(flag,flag?MessageConstant.EDIT_CHECKITEM_SUCCESS:MessageConstant.EDIT_CHECKITEM_FAIL);
    }

    /**
     * 查询检查项（分页查询）
     * @param queryPageBean 页面数，页面条数，查询条件
     * @return 数据总条数，当前页数据
     */
    @PostMapping("findPage.do")
    public PageResult get(@RequestBody QueryPageBean queryPageBean) {
        return checkItemService.findByCondition(queryPageBean);
    }

    /**
     * 查询检查项（通过id）
     * @param id 需要查询的检查项id
     * @return flag，message，检查项数据
     */
    @GetMapping("findById.do")
    public Result findById(Integer id) {
        CheckItem checkItem = checkItemService.findById(id);
        return new Result(checkItem != null, checkItem != null ? MessageConstant.EDIT_CHECKITEM_SUCCESS : MessageConstant.ADD_CHECKITEM_FAIL, checkItem);
    }
}
